package com.example.wechat;

import android.content.Context;
import android.net.Uri;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MessageRepository {
    private final WebSocketService webSocketService;
    private final ExecutorService executorService;
    private final Context context;
    private final MutableLiveData<List<Message>> messagesLiveData;
    private final MutableLiveData<Integer> unreadCountLiveData;

    public MessageRepository(Context context, WebSocketService webSocketService) {
        this.context = context.getApplicationContext();
        this.webSocketService = webSocketService;
        this.executorService = Executors.newFixedThreadPool(4);
        this.messagesLiveData = new MutableLiveData<>(new ArrayList<>());
        this.unreadCountLiveData = new MutableLiveData<>(0);
    }

    public LiveData<List<Message>> getMessages(String userId1, String userId2) {
        // TODO: 从阿里云数据库获取消息
        return messagesLiveData;
    }

    public void sendTextMessage(String senderId, String receiverId, String content) {
        executorService.execute(() -> {
            Message message = new Message();
            message.setSenderId(senderId);
            message.setReceiverId(receiverId);
            message.setType(Message.TYPE_TEXT);
            message.setContent(content);
            message.setStatus(Message.STATUS_SENDING);

            // TODO: 保存到阿里云数据库
            webSocketService.sendMessage(content);
        });
    }

    public void sendImageMessage(String senderId, String receiverId, Uri imageUri) {
        executorService.execute(() -> {
            Message message = new Message();
            message.setSenderId(senderId);
            message.setReceiverId(receiverId);
            message.setType(Message.TYPE_IMAGE);
            message.setStatus(Message.STATUS_SENDING);

            // TODO: 保存到阿里云数据库

            ImageCompressUtils.compressImage(context, imageUri, new ImageCompressUtils.CompressCallback() {
                @Override
                public void onSuccess(File compressedFile) {
                    // TODO: 上传图片到阿里云并获取URL
                    String imageUrl = "http://your-server.com/images/" + compressedFile.getName();
                    message.setImageUrl(imageUrl);
                    // TODO: 更新阿里云数据库中的消息
                    webSocketService.sendImage(imageUrl);
                }

                @Override
                public void onError(String error) {
                    message.setStatus(Message.STATUS_FAILED);
                    // TODO: 更新阿里云数据库中的消息状态
                }
            });
        });
    }

    public void sendVoiceMessage(String senderId, String receiverId, String voiceUrl, int duration) {
        executorService.execute(() -> {
            Message message = new Message();
            message.setSenderId(senderId);
            message.setReceiverId(receiverId);
            message.setType(Message.TYPE_VOICE);
            message.setVoiceUrl(voiceUrl);
            message.setVoiceDuration(duration);
            message.setStatus(Message.STATUS_SENDING);

            // TODO: 保存到阿里云数据库
            webSocketService.sendVoice(voiceUrl, duration);
        });
    }

    public void updateMessageStatus(String messageId, int status) {
        executorService.execute(() -> {
            // TODO: 更新阿里云数据库中的消息状态
        });
    }

    public void markMessageAsRead(String messageId) {
        executorService.execute(() -> {
            // TODO: 更新阿里云数据库中的消息已读状态
        });
    }

    public LiveData<Integer> getUnreadMessageCount(String userId) {
        // TODO: 从阿里云数据库获取未读消息数
        return unreadCountLiveData;
    }

    public void retryMessage(Message message) {
        executorService.execute(() -> {
            message.setStatus(Message.STATUS_SENDING);
            // TODO: 更新阿里云数据库中的消息状态

            switch (message.getType()) {
                case Message.TYPE_TEXT:
                    webSocketService.sendMessage(message.getContent());
                    break;
                case Message.TYPE_IMAGE:
                    webSocketService.sendImage(message.getImageUrl());
                    break;
                case Message.TYPE_VOICE:
                    webSocketService.sendVoice(message.getVoiceUrl(), message.getVoiceDuration());
                    break;
            }
        });
    }

    public void release() {
        executorService.shutdown();
    }
} 